package edu.harding.redzone;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PreviewModeView extends SurfaceView implements
		SurfaceHolder.Callback
{
	private static Play mPlay;
	private static int mPlayType;
	private static Paint mPaint = new Paint();
	private ViewThread mThread;
	private int mColor;
	private int mPlayerColor;
	private static Bitmap mBitmap;
	private static int mWidth;
	private static int mHeight;

	public static Paint Paint()
	{
		return mPaint;
	}

	public static Bitmap Bitmap()
	{
		return mBitmap;
	}

	public static int Width()
	{
		return mWidth;
	}

	public static int Height()
	{
		return mHeight;
	}

	public PreviewModeView(Context context)
	{
		super(context);
		initializeThread();
	}

	public PreviewModeView(Context context, AttributeSet attr)
	{
		super(context, attr);
		initializeThread();
	}

	public PreviewModeView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		initializeThread();
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height)
	{
		mWidth = width;
		mHeight = height;
	}

	public void surfaceCreated(SurfaceHolder holder)
	{
		if (!mThread.isAlive())
		{
			mThread = new ViewThread(this);
			mThread.setRunning(true);
			mThread.start();
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder)
	{
		if (mThread.isAlive())
		{
			mThread.setRunning(false);
		}
	}

	private int PlayerColor(String color)
	{
		if (color.equals("1"))
		{
			mColor = 1;
			mPlayerColor = R.drawable.black;
		} else if (color.equals("2"))
		{
			mColor = 2;
			mPlayerColor = R.drawable.blue;
		} else if (color.equals("3"))
		{
			mColor = 3;
			mPlayerColor = R.drawable.cyan;
		} else if (color.equals("4"))
		{
			mColor = 4;
			mPlayerColor = R.drawable.gray;
		} else if (color.equals("5"))
		{
			mColor = 5;
			mPlayerColor = R.drawable.green;
		} else if (color.equals("6"))
		{
			mColor = 6;
			mPlayerColor = R.drawable.red;
		} else if (color.equals("7"))
		{
			mColor = 7;
			mPlayerColor = R.drawable.yellow;
		}
		RouteColor();

		return mPlayerColor;
	}

	private int RouteColor()
	{
		switch (mColor)
		{
		case 1:
			return Color.BLACK;
		case 2:
			return Color.BLUE;
		case 3:
			return Color.CYAN;
		case 4:
			return Color.GRAY;
		case 5:
			return Color.GREEN;
		case 6:
			return Color.RED;
		case 7:
			return Color.YELLOW;
		}
		return (Integer) null;
	}

	private void initializeThread()
	{
		getHolder().addCallback(this);
		mThread = new ViewThread(this);
		initializePaint();
	}

	private void initializePaint()
	{
		setFocusable(true);

		mBitmap = BitmapFactory.decodeResource(getResources(),
				PlayerColor(MainActivity.PlayerColor()));

		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setColor(RouteColor());
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(5);

	}

	public void doDraw(Canvas canvas)
	{
		canvas.drawColor(Color.WHITE);
		if (PreviewModeActivity.Deleted() == false)
		{
			synchronized (mPlay)
			{
				for (Player player : mPlay.Players())
					player.doDraw(canvas, mPaint, mBitmap);
			}
		}
	}

	public static Play PlayToSave()
	{
		return mPlay;
	}

	public static String PlayToSend()
	{
		return mPlay.Name();
	}

	public static Play PlayToEdit()
	{
		return mPlay;
	}

	public static Play PlayToDelete()
	{
		return mPlay;
	}

	public static int PlayType()
	{
		return mPlayType;
	}

	public static void PlayType(int type)
	{
		mPlayType = type;
	}

	public static void PlayToView(Play play)
	{
		mPlay = play;
	}

}
